package com.xd.core.api.consumer;

import com.xd.common.constant.SysConstant;
import com.xd.core.api.dto.DelayOfflinePayDto;
import com.xd.core.api.entity.RepaymentOrderInfo;
import com.xd.core.api.mapper.RepaymentOrderInfoMapper;
import com.xd.core.business.mapper.RepaymentScheduleMapper;
import com.xd.core.debt.mapper.DebtMapper;
import com.xd.core.note.IAsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RabbitListener(queues = "delay.queue")
public class DelayConsumerController {
    @Autowired
    private RepaymentOrderInfoMapper repaymentOrderInfoMapper;
    @Autowired
    private RepaymentScheduleMapper repaymentScheduleMapper;
    @Autowired
    private IAsyncService iAsyncService;
    @Autowired
    private DebtMapper debtMapper;

    //线下还款 超过4小时视为超时失败
    @RabbitHandler
    public void DelayOfflinePay(DelayOfflinePayDto delayOfflinePayDto) {
        String repaymentOrderNo = delayOfflinePayDto.getRepaymentOrderNo();
        log.info("线下还款超时失败：{}", repaymentOrderNo);
        RepaymentOrderInfo old = repaymentOrderInfoMapper.selectById(repaymentOrderNo);
        //如果8小时后还在申请中代表超时失败
        if (old.getPayStatus().equals(SysConstant.CREATE_ORDER_STATUS_ONE)) {
            if (delayOfflinePayDto.getType().equals(SysConstant.DEDUCT_MONEY_TYPE_THREE)) {
                repaymentScheduleMapper.updateFailCause(delayOfflinePayDto.getOrderId());
            } else {
                debtMapper.updateFailCause(delayOfflinePayDto.getDebtId());
            }
            repaymentOrderInfoMapper.updateStatus(repaymentOrderNo, "3","线下还款超时失败", null);
            iAsyncService.advanceSinglePay(old, "3", "线下还款超时失败");
        }
    }
}
